import random
import importlib

from .log.log import Logger
from config import settings

logger = Logger().logger


def get_function_from_path_string(path_string: str):
    if path_string == settings.undefined:
        return path_string
    module_path, function = path_string.rsplit('.', 1)
    try:
        module = importlib.import_module(module_path)
    except ModuleNotFoundError:
        logger.error(f"[{module_path}]模块没有找到~!")
    else:
        if hasattr(module, function):
            return getattr(module, function)
        else:
            logger.error(f"[{path_string}]方法没有找到~!")

    return None


def random_ua():
    ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{} Safari/537.36'

    v1 = random.choice([_ for _ in range(90, 107)])
    v2 = random.choice([_ for _ in range(10)])
    v3 = random.choice([_ for _ in range(1000, 9999)])
    v4 = random.choice([_ for _ in range(10, 99)])

    version = f'{v1}.{v2}.{v3}.{v4}'
    return ua.format(version)


def heartbeat(*args, **kwargs):
    """
    程序心跳方法
    :return:
    """
    # logger.info("心跳一下")
